package test.test6_21;

import java.util.Arrays;
import java.util.Scanner;

/*
其次，请尝试完成上课时未完成的人名数组的新增操作。
如果有能力，可以继续做完删除，改，查
（查出目标元素的下标）。
 */
public class Demo06 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String name = sc.next();
        String[] arr = {"111", "222", "333", "444", "555", "666", "777", "888", "999"};

        if (deletePeople(arr, name) == null) {
            System.out.println("查无此人！！！");
        } else {
            System.out.println("删除成功");
            System.out.println(Arrays.toString(deletePeople(arr, name)));
        }
        System.out.println("****************************************");
        int result = updatePeople(arr, "444", "110");
        if (result == 1) {
            System.out.println("修改成功");
        } else {
            System.out.println("修改失败");
        }
        System.out.println("result = " + result);
        System.out.println(Arrays.toString(arr));
        System.out.println("****************************************");
        readPeople(arr,"999");
    }

    /*
    删除操作
     */
    public static String[] deletePeople(String[] arr, String name) {

        //遍历一遍数组中的元素看是否有需要删除的这个元素

        int count = 0;//用于记录需要删除的元素出现的个数

        for (String s : arr) {
            if (name.equals(s)) {
                count++;
            }
        }
        if (count == 0) {
            return null;
        }


        int length = arr.length - count;
        String[] newArray = new String[length];

        int index = 0;
        //遍历老数组
        for (String s : arr) {
            if (name.equals(s)) {
                continue;
            }
            newArray[index++] = s;
        }

        return newArray;

    }

    public static int updatePeople(String[] arr, String name, String updateName) {

        int x = 0;//1代表更新成功 0代表查无此人

        for (int i = 0; i < arr.length; i++) {
            if (name.equals(arr[i])) {
                arr[i] = updateName;
                x = 1;
                break;
            }
        }
        return x;
    }

    public static void readPeople(String[] arr, String name) {

        int x = 1;
        for (int i = 0; i < arr.length; i++) {
            if (name.equals(arr[i])) {
                System.out.println("找到了,他是第"+i+"个");
                x = 0;
                break;
            }
        }
        if (x == 1) {
            System.out.println("查无此人");
        }


    }

}
